package com.ruoyi.framework.encryption;

import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayOutputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.crypto.Cipher;

/**
 *
 */

/**
 * <p>
 * RSA公钥/私钥/签名工具包
 * </p>
 * <p>
 * 罗纳德·李维斯特（Ron [R]ivest）、阿迪·萨莫尔（Adi [S]hamir）和伦纳德·阿德曼（Leonard [A]dleman）
 * </p>
 * <p>
 * 字符串格式的密钥在未在特殊说明情况下都为BASE64编码格式<br/>
 * 由于非对称加密速度极其缓慢，一般文件不使用它来加密而是使用对称加密，<br/>
 * 非对称加密算法可以用来对对称加密的密钥加密，这样保证密钥的安全也就保证了数据的安全
 * </p>
 *
 * @author monkey
 * @date 2018-10-29
 */
public class RSAUtils {

    /**
     * 加密算法RSA
     */
    public static final String KEY_ALGORITHM = "RSA";

    /**
     * 签名算法
     */
    public static final String SIGNATURE_ALGORITHM = "MD5withRSA";

    /**
     * 获取公钥的key
     */
    private static final String PUBLIC_KEY = "RSAPublicKey";

    /**
     * 获取私钥的key
     */
    private static final String PRIVATE_KEY = "RSAPrivateKey";

    /**
     * RSA最大加密明文大小
     */
    private static final int MAX_ENCRYPT_BLOCK = 117;

    /**
     * RSA最大解密密文大小
     */
    private static final int MAX_DECRYPT_BLOCK = 128;

    /**
     * RSA 位数 如果采用2048 上面最大加密和最大解密则须填写:  245 256
     */
    private static final int INITIALIZE_LENGTH = 1024;


    /**
     * 创建指定位数的随机字符串
     *
     * @param length 表示生成字符串的长度
     * @return 字符串
     */
    public static String getRandomString(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 生成密钥对(公钥和私钥)
     *
     * @return
     * @throws Exception
     */
    public static Map<String, Object> genKeyPair() throws Exception {
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
        keyPairGen.initialize(INITIALIZE_LENGTH);
        KeyPair keyPair = keyPairGen.generateKeyPair();
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        Map<String, Object> keyMap = new HashMap<String, Object>(2);
        keyMap.put(PUBLIC_KEY, publicKey);
        keyMap.put(PRIVATE_KEY, privateKey);
        return keyMap;
    }

    /**
     * 用私钥对信息生成数字签名
     *
     * @param data 已加密数据
     * @param privateKey 私钥(BASE64编码)
     *
     * @return
     * @throws Exception
     */
    public static String sign(byte[] data, String privateKey) throws Exception {
        byte[] keyBytes = Base64.decodeBase64(privateKey);
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
        Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
        signature.initSign(privateK);
        signature.update(data);
        return Base64.encodeBase64String(signature.sign());
    }

    /**
     * 校验数字签名
     *
     * @param data
     *            已加密数据
     * @param publicKey
     *            公钥(BASE64编码)
     * @param sign
     *            数字签名
     * @return
     * @throws Exception
     *
     */
    public static boolean verify(byte[] data, String publicKey, String sign) throws Exception {
        byte[] keyBytes = Base64.decodeBase64(publicKey);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        PublicKey publicK = keyFactory.generatePublic(keySpec);
        Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
        signature.initVerify(publicK);
        signature.update(data);
        return signature.verify(Base64.decodeBase64(sign));
    }


    public static void main(String[] args) {
        String strData = "这是加的一方的说法水电费第三方！";
        try {
            Map<String, Object> stringObjectMap = genKeyPair();
            String privateKey = getPrivateKey(stringObjectMap);
            String publicKey = getPublicKey(stringObjectMap);

            System.out.println("私钥：" + privateKey);
            System.out.println("公钥：" + publicKey);


//            //验签流程
//            //签名
//            byte[] leshuBytes = "河北乐树汇泉电子科技有限公司".getBytes();
//            String sign = sign(leshuBytes, privateKey);
//            boolean verify = verify(leshuBytes, publicKey, sign);
//            System.out.println("数字签名：" + verify + sign);
//
//
//            byte[] bytes = encryptByPublicKey(strData.getBytes(), publicKey);
//            byte[] str = decryptByPrivateKey(bytes, privateKey);
//
//            String s = decryptDataOnJava(Base64.encodeBase64String(bytes), privateKey);
//            System.out.println(new String(str));
//            System.out.println(s);

            String str = "nihaa 你好吗？fsdfsdfdsffas第三方的所发生的法撒旦法撒旦法";

            //私钥加密
            Map<String, String> stringStringMap = privateKeyEncryptString(str, privateKey);
            //公钥解密
            String encryptedData = publicKeyDecryptString(stringStringMap, publicKey);
            System.out.println(encryptedData);
            //公钥加密
            Map<String, String> stringStringMap1 = publicKeyEncryptString(str, publicKey);
            //私钥解密
            String encryptedData1 = privateKeyDecryptString(stringStringMap1, privateKey, "utf-8");
            System.out.println(encryptedData1);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     *     AES、公钥加密
     * @author XiaoYi...
     * @param str  待加密数据
     * @param publicKey 公钥
     * @date 2020/7/27 15:54
     * @return 加密后的数据
     */
    public static Map<String, String> publicKeyEncryptString(String str, String publicKey) {
        //获取AES 随机秘钥
        String AESrandomString = getRandomString(16);
        Map<String, String> map = new HashMap<>();
        try {
            //加密后的数据
            String encryptData = AesEncryptUtils.encrypt(str, AESrandomString);

            //通过 RSA公钥 对AES秘钥加密
            String RSAAES = encryptedDataOnJava(AESrandomString, publicKey);

            map.put("encrypted", RSAAES);
            map.put("requestData", encryptData);
            return map;
        } catch (Exception e) {
            System.out.println("公钥加密异常！");
        }
        return null;
    }


    /**
     *     AES、公钥加密（16进制字符串）
     * @author XiaoYi...
     * @param str  待加密数据
     * @param publicKey 公钥
     * @date 2020/7/27 15:54
     * @return 加密后的数据
     */
    public static Map<String, String> publicKeyEncryptStringHex(String str, String publicKey) {
        //获取AES 随机秘钥
        String AESrandomString = getRandomString(16);
        Map<String, String> map = new HashMap<>();
        try {
            //加密后的数据
            String encryptData = AesEncryptUtils.encryptHex(str, AESrandomString);

            //通过 RSA公钥 对AES秘钥加密
            String RSAAES = encryptedDataOnJava(AESrandomString, publicKey);

            map.put("encrypted", RSAAES);
            map.put("requestData", encryptData);
            return map;
        } catch (Exception e) {
            System.out.println("公钥加密异常！");
        }
        return null;
    }


    // AES、私钥解密
    public static String privateKeyDecryptString(Map<String, String> data, String privateKey, String AESDecryptionCharacterSet) {
        try {
            //通过 公钥对加密的AES秘钥解密
            String AESrandomString = decryptPrivateDataOnJava(data.get("encrypted"), privateKey);
//            System.out.println("解密的AES：" + AESrandomString);
            String encrypted = AesEncryptUtils.decrypt((String) data.get("requestData"), AESrandomString, AESDecryptionCharacterSet);
            return encrypted;
        } catch (Exception e) {
            System.out.println("公钥解密异常！");
        }
        return null;
    }


    // AES、私钥解密（字符串是16进制）
    public static String privateKeyDecryptStringHex(Map<String, String> data, String privateKey, String AESDecryptionCharacterSet) {
        try {
            //通过 公钥对加密的AES秘钥解密
            String AESrandomString = decryptPrivateDataOnJava(data.get("encrypted"), privateKey);
//            System.out.println("解密的AES：" + AESrandomString);
            String encrypted = AesEncryptUtils.decryptHex((String) data.get("requestData"), AESrandomString, AESDecryptionCharacterSet);
            return encrypted;
        } catch (Exception e) {
            System.out.println("公钥解密异常！");
        }
        return null;
    }


    /**
     *     AES、私钥加密
     * @author XiaoYi...
     * @param str  待加密数据
     * @param privateKey 私钥
     * @date 2020/7/27 15:54
     * @return 加密后的数据
     */
    public static Map<String, String> privateKeyEncryptString(String str, String privateKey) {
        //获取AES 随机秘钥
        String AESrandomString = getRandomString(16);
        Map<String, String> map = new HashMap<>();
//        System.out.println("加密的AES:" + AESrandomString);
        //加密字符串
        try {
            //加密后的数据
            String encryptData = AesEncryptUtils.encrypt(str, AESrandomString);

            //通过 RSA私钥 对AES秘钥加密
            String RSAAES = encryptedPrivateDataOnJava(AESrandomString, privateKey);

            map.put("encrypted", RSAAES);
            map.put("requestData", encryptData);
            return map;
        } catch (Exception e) {
            System.out.println("私钥加密异常！");
        }
        System.out.println("私钥加密失败");
        return null;
    }


    // AES、公钥解密
    public static String publicKeyDecryptString(Map<String, String> data, String publicKey) {
        try {
            //通过 公钥对加密的AES秘钥解密
            String AESrandomString = decryptPublickDataOnJava(data.get("encrypted"), publicKey);
//            System.out.println("解密的AES：" + AESrandomString);
            String encrypted = AesEncryptUtils.decrypt((String) data.get("requestData"), AESrandomString,
                    "utf-8");
            return encrypted;
        } catch (Exception e) {
            System.out.println("公钥解密异常！");
        }
        return null;
    }


    // AES、公钥解密（字符串是16进制）
    public static String publicKeyDecryptStringHex(Map<String, String> data, String publicKey) {
        try {
            //通过 公钥对加密的AES秘钥解密
            String AESrandomString = decryptPublickDataOnJava(data.get("encrypted"), publicKey);
//            System.out.println("解密的AES：" + AESrandomString);
            String encrypted = AesEncryptUtils.decryptHex((String) data.get("requestData"), AESrandomString,
                    "utf-8");
            return encrypted;
        } catch (Exception e) {
            System.out.println("公钥解密异常！");
        }
        return null;
    }


    /**
     * 私钥解密
     *
     * @param encryptedData 已加密数据
     * @param privateKey 私钥(BASE64编码)
     * @return
     * @throws Exception
     */
    public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) throws Exception {
        byte[] keyBytes = Base64.decodeBase64(privateKey);
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, privateK);
        int inputLen = encryptedData.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段解密
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
                cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_DECRYPT_BLOCK;
        }
        byte[] decryptedData = out.toByteArray();
        out.close();
        return decryptedData;
    }


    /**
     * 公钥解密
     * @param encryptedData 已加密数据
     * @param publicKey 公钥(BASE64编码)
     * @return
     * @throws Exception
     */
    public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) throws Exception {
        byte[] keyBytes = Base64.decodeBase64(publicKey);
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key publicK = keyFactory.generatePublic(x509KeySpec);
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, publicK);
        int inputLen = encryptedData.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段解密
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
                cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_DECRYPT_BLOCK;
        }
        byte[] decryptedData = out.toByteArray();
        out.close();
        return decryptedData;
    }

    /**
     * 公钥加密
     *
     * @param data 源数据
     * @param publicKey 公钥(BASE64编码)
     * @return
     * @throws Exception
     */
    public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
        byte[] keyBytes = Base64.decodeBase64(publicKey);
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key publicK = keyFactory.generatePublic(x509KeySpec);
        // 对数据加密
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, publicK);
        int inputLen = data.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段加密
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_ENCRYPT_BLOCK;
        }
        byte[] encryptedData = out.toByteArray();
        out.close();
        return encryptedData;
    }

    /**
     * 私钥加密
     * @param data 源数据
     * @param privateKey 私钥(BASE64编码)
     * @return
     * @throws Exception
     */
    public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws Exception {
        byte[] keyBytes = Base64.decodeBase64(privateKey);
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, privateK);
        int inputLen = data.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段加密
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_ENCRYPT_BLOCK;
        }
        byte[] encryptedData = out.toByteArray();
        out.close();
        return encryptedData;
    }

    /**
     * 获取私钥
     * @param keyMap 密钥对
     * @return
     * @throws Exception
     */
    public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {
        Key key = (Key) keyMap.get(PRIVATE_KEY);
        return Base64.encodeBase64String(key.getEncoded());
    }

    /**
     * 获取公钥
     * @param keyMap
     *            密钥对
     * @return
     * @throws Exception
     */
    public static String getPublicKey(Map<String, Object> keyMap) throws Exception {
        Key key = (Key) keyMap.get(PUBLIC_KEY);
        return Base64.encodeBase64String(key.getEncoded());
    }

    /**
     * java端公钥加密
     */
    public static String encryptedDataOnJava(String data, String PUBLICKEY) {
        try {
            data = Base64.encodeBase64String(encryptByPublicKey(data.getBytes(), PUBLICKEY));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return data;
    }

    /**
     * java端公钥加密
     */
    public static String encryptedPublickDataOnJava(String data, String PUBLICKEY) {
        try {
            data = Base64.encodeBase64String(encryptByPublicKey(data.getBytes(), PUBLICKEY));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return data;
    }
    /**
     * java端私钥加密
     */
    public static String encryptedPrivateDataOnJava(String data, String PRIVATEKEY) {
        try {
            data = Base64.encodeBase64String(encryptByPrivateKey(data.getBytes(), PRIVATEKEY));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return data;
    }


    /**
     * java端公钥解密
     */
    public static String decryptPublickDataOnJava(String data, String PUBLICKEY) {
        String temp = "";
        try {
            byte[] rs = Base64.decodeBase64(data);
            temp = new String(RSAUtils.decryptByPublicKey(rs, PUBLICKEY), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return temp;
    }

    /**
     * java端私钥解密
     */
    public static String decryptPrivateDataOnJava(String data, String PRIVATEKEY) {
        String temp = "";
        try {
            byte[] rs = Base64.decodeBase64(data);
            temp = new String(RSAUtils.decryptByPrivateKey(rs, PRIVATEKEY), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return temp;
    }
}